package com.ostraffel.recallvocabularybackend.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ostraffel.recallvocabularybackend.DTO.ApplicationDictDTO;
import com.ostraffel.recallvocabularybackend.DTO.UserConfig;
import com.ostraffel.recallvocabularybackend.DTO.UserConfigChangeRecallNumber;
import com.ostraffel.recallvocabularybackend.DTO.UserConfigDetailed;
import com.ostraffel.recallvocabularybackend.DTO.Word;
import com.ostraffel.recallvocabularybackend.TempTest.TempTest;
import com.ostraffel.recallvocabularybackend.service.BusinessService;


@CrossOrigin
@RestController
@RequestMapping("/api")
public class BusinessController {

    @Autowired
    TempTest tempTest;

    @Autowired
    BusinessService businessService;

    @PostMapping("/get-recall-words")
    public List<Word> GetRecallWords(@RequestBody UserConfig userConfig) {
        System.out.println("GetRecallWords: " + userConfig.toString());
        List<Word> words = tempTest.CreateWords();
        return words;
    }

    @PostMapping("/search-user-config")
    public UserConfig GetUserConfig(@RequestBody String userId) {
        UserConfig userConfig = tempTest.SearchUserConfig(userId);
        return userConfig;
    }

    @PostMapping("/add-user")
    public ResponseEntity<?> AddUser(@RequestBody UserConfigDetailed userConfigDetailed) {
        try {
            System.out.println("有请求进来了!");
            tempTest.AddUserService(userConfigDetailed);
            System.out.println("添加成功!");
            return ResponseEntity.badRequest().body(Map.of(
                    "status", "error",
                    "code", 200,
                    "message", "ok"
            ));
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(Map.of(
                    "status", "error",
                    "code", 400,
                    "message", e.getMessage()
            ));
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body(Map.of(
                    "status", "error",
                    "code", 500,
                    "message", "用户添加失败: " + e.getMessage()
            ));
        }
    }

    @PostMapping("/application-dict")
    public ResponseEntity<?> ApplicationDict(@RequestBody ApplicationDictDTO applicationDictDTO) {
        try {
            System.out.println("有请求进来了!");
            tempTest.GnerateUserTableService(applicationDictDTO);
            System.out.println("添加成功!");
            return ResponseEntity.badRequest().body(Map.of(
                    "status", "error",
                    "code", 200,
                    "message", "ok"
            ));
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(Map.of(
                    "status", "error",
                    "code", 400,
                    "message", e.getMessage()
            ));
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body(Map.of(
                    "status", "error",
                    "code", 500,
                    "message", "文件上传失败: " + e.getMessage()
            ));
        }
    }

    @PostMapping("/set-recall-number")
    public ResponseEntity<?> SetRecallNumber(@RequestBody UserConfigChangeRecallNumber userConfigChangeRecallNumber) {
        try {
            System.out.println("有请求进来了!");
            businessService.UpdateUserConfig(userConfigChangeRecallNumber);
            System.out.println("添加成功!");
            return ResponseEntity.badRequest().body(Map.of(
                    "status", "error",
                    "code", 200,
                    "message", "ok"
            ));
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(Map.of(
                    "status", "error",
                    "code", 400,
                    "message", e.getMessage()
            ));
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body(Map.of(
                    "status", "error",
                    "code", 500,
                    "message", "文件上传失败: " + e.getMessage()
            ));
        }
    }


    /**
     * 接收文件并保存到指定目录
     * @param file
     * @param dictionaryName
     * @return
     */
    @PostMapping("/upload-file")
    public ResponseEntity<?> ReceiveWordList(@RequestParam("file") MultipartFile file,
                                            @RequestParam String dictionaryName) {   
        try {
            tempTest.PutListInDir(file, dictionaryName);
            Map<String, Object> response = new HashMap<>();
            response.put("status", "success");
            response.put("code", 200);
            response.put("data", Map.of(
                    "originalName", file.getOriginalFilename(),
                    "fileSize", file.getSize()
            ));
            
            return ResponseEntity.ok(response);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(Map.of(
                    "status", "error",
                    "code", 400,
                    "message", e.getMessage()
            ));
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body(Map.of(
                    "status", "error",
                    "code", 500,
                    "message", "文件上传失败: " + e.getMessage()
            ));
        }
    }
    
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, Spring Boot!";
    }
    @GetMapping("/senior")
    public String TransportWordsController() {
        tempTest.TransportWordsService();
        return "Hello, Spring Boot!";
    }
}
